package com.lzw.bluetoothpractice.Adapters;


import android.content.Context;
import android.graphics.Bitmap;
import android.text.SpannableString;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.lzw.bluetoothpractice.Models._User;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.FaceTextUtils;
import com.lzw.bluetoothpractice.Utils.TimeUtils;

import java.util.List;

import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.v3.BmobConfig;
import cn.bmob.v3.BmobUser;

import static cn.bmob.newim.core.BmobIMClient.getContext;

/**
*
*@atuthor lzw
*create at
*/
public class SendMessageAdapter extends BaseListAdapter<BmobIMMessage> {

    private final int TYPE_RECEIVER_TXT = 0;
    private final int TYPE_SEND_TXT = 1;
    private String currentObjectId;

    private Context mcontext;
    public SendMessageAdapter(List<BmobIMMessage> list, Context mcontext) {
        super(mcontext, list);
        this.mcontext = mcontext;
        currentObjectId=BmobUser.getCurrentUser(_User.class).getObjectId();
    }

    @Override
    public int getItemViewType(int position) {
        BmobIMMessage msg = list.get(position);

        return msg.getFromId().equals(currentObjectId) ? TYPE_SEND_TXT: TYPE_RECEIVER_TXT;

    }
    @Override
    public int getViewTypeCount() {
        return list.size();
    }
    private View createViewByType(BmobIMMessage message, int position) {

        return getItemViewType(position) == TYPE_RECEIVER_TXT ?
                    mInflater.inflate(R.layout.item_text_receive, null)
                    :
                    mInflater.inflate(R.layout.item_text_send, null);
    }


    @Override
    public View bindView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final BmobIMMessage item = list.get(position);
        if (convertView == null) {
            convertView = createViewByType(item, position);
        }
        //文本类型
        ImageView iv_avatar = ViewHolder.get(convertView, R.id.iv_head);
        //final ImageView iv_fail_resend = ViewHolder.get(convertView, R.id.chat_item_fail);//失败重发
        //   final TextView tv_send_status = ViewHolder.get(convertView, R.id.chat_item_progress);//发送状态
        TextView tv_time = ViewHolder.get(convertView, R.id.item_tv_time);
        TextView tv_message = ViewHolder.get(convertView, R.id.chat_item_content_text);
        tv_message.setText(item.getContent());
        /*try {
            SpannableString spannableString = FaceTextUtils
                    .toSpannableString(mContext, text);
            tv_message.setText(spannableString);
        } catch (Exception e) {
        }*/
        tv_time.setText(TimeUtils.getFormatHMS(item.getCreateTime()));
        Glide.with(mcontext).load(BmobUser.getCurrentUser(_User.class).getHead()).into(iv_avatar);
        return convertView;

    }
}
